#include <bits/stdc++.h>

using namespace std;

class ListNode
{
public:
    int val;
    ListNode *next;
    ListNode(int val)
    {
        this->val = val;
        this->next = NULL;
    }
};

void printListNode(ListNode * head){
    if(head == NULL)
        printf("NULL");
    while(head != NULL){
        printf("%d\t", head->val);
        head = head->next;
    }
    printf("\n");
}

/**
     * @param head: a ListNode
     * @param val: An integer
     * @return: a ListNode
     */
ListNode * removeElements(ListNode * head, int val)
{
    // write your code here
    while(head != NULL && head->val == val)
        head = head->next;
    ListNode * phead = head;
    if(phead == NULL)
        return phead;
//    printf("%d\t", head->val);
    while(phead != NULL && phead->next != NULL){
//        printf("%d%d\n", phead->val, phead->next->val);
        while(phead->next != NULL && phead->next->val == val)
            phead->next = phead->next->next;
        phead = phead->next;
    }
    return head;
}

int main()
{
    ListNode *head = new ListNode(1);
    head->next = new ListNode(2);
    head->next->next = new ListNode(3);
    head->next->next->next = new ListNode(3);
    head->next->next->next->next = new ListNode(4);
    head->next->next->next->next->next = new ListNode(5);
    head->next->next->next->next->next->next = new ListNode(3);

    printListNode(head);
    printListNode(removeElements(head, 3));
    return 0;
}

/**
删除链表中的元素

删除链表中等于给定值val的所有节点。
样例

给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表：1->2->4->5。
*/
